package com.game;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener, ActionListener {
    boolean falg = false;
    String path = "image/animal/animal3/";
    int[][] data = new int[4][4];
    int x = 3,y = 3,step = 0;  //空白块初始坐标

    JMenuItem replayItem = new JMenuItem("重新游戏");
    JMenuItem reLoginItem = new JMenuItem("重新登录");
    JMenuItem closeItem = new JMenuItem("关闭游戏");
    JMenuItem animalItem = new JMenuItem("Animal");
    JMenuItem girlItem = new JMenuItem("Girl");
    JMenuItem sportItem = new JMenuItem("Sport");
    JMenuItem changePicturetItem = new JMenuItem("更换图片");

    JMenuItem accountItem = new JMenuItem("联系方式");

    //空参构造
    public GameJFrame(){    //无参构造
        initJFrame();//界面初始化方法

        initJMenuBar();//菜单栏初始化

        //初始化二维数组
        initData();

        //添加拼图图片
        initImage();

        //展示界面
        this.setVisible(true);
    }

    //打乱数组
    private void initData() {
        Random ran = new Random();
        int[] ranArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0};
        for (int i = 0; i < 15; i++) { //随机顺序一维数组
            int index = ran.nextInt(ranArr.length-1);
            int temp = ranArr[i];
            ranArr[i] = ranArr[index];
            ranArr[index] = temp;
        }
        for (int i = 0; i < 15; i++) {
            if (ranArr[i] != i+1){ //如果找到一个跟ranArr不一样的
                //给二维数组赋值
                for (int j = 0; j < ranArr.length; j++) {
                    data[j/4][j%4] = ranArr[j];
                }
                return;
            }
        }
        initData();
    }

    //加载图片
    private void initImage() {
        //清除所有图片
        this.getContentPane().removeAll();

        victory();
        //如果胜利
        if (falg){
            JLabel jLabel = new JLabel(new ImageIcon("image/win.png"));
            jLabel.setBounds(203, 283, 197, 73);
            this.add(jLabel);
        }

        JLabel stepCount = new JLabel("步数："+step);
        stepCount.setBounds(50, 30, 100, 20);
        this.add(stepCount);

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                //创建图片对象存到管理容器对象，将管理容器加载到界面
                JLabel jLabel = new JLabel(new ImageIcon(path+data[i][j]+".jpg"));

                //设置坐标
                jLabel.setBounds(j*105 + 83,i*105 + 136,105, 105);
                //设置边框凸起
                jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));

                //获取JFrame容器对象并放置图片管理容器
                this.getContentPane().add(jLabel);
            }
        }

        JLabel background = new JLabel(new ImageIcon("image/background.png"));
        background.setBounds(40, 40, 508, 560);
        this.getContentPane().add(background);

        //重新生成
        this.getContentPane().repaint();
    }

    //初始化菜单
    private void initJMenuBar() {
        //创建菜单栏对象
        JMenuBar jMenuBar = new JMenuBar();

        //创建菜单上选项对象
        JMenu functionJMenu = new JMenu("功能");
        JMenu replacementMenu = new JMenu("更换");
        JMenu replacementTempMenu = new JMenu("更换模块");
        JMenu aboutJMenu = new JMenu("关于我们");


        //添加事件监听
        replayItem.addActionListener(this);
        reLoginItem.addActionListener(this);
        closeItem.addActionListener(this);
        animalItem.addActionListener(this);
        girlItem.addActionListener(this);
        sportItem.addActionListener(this);
        changePicturetItem.addActionListener(this);
        accountItem.addActionListener(this);



        //给选项添加条目
        functionJMenu.add(replayItem);  //功能 选项
        functionJMenu.add(reLoginItem);
        functionJMenu.add(closeItem);

        aboutJMenu.add(accountItem);    //联系我们 选项

        replacementMenu.add(replacementTempMenu);   //更换 选项 增添更换模块功能
        replacementMenu.add(changePicturetItem);

        replacementTempMenu.add(animalItem);    //更换模块 选项
        replacementTempMenu.add(girlItem);
        replacementTempMenu.add(sportItem);

        //给菜单添加选项对象
        jMenuBar.add(functionJMenu);
        jMenuBar.add(replacementMenu);
        jMenuBar.add(aboutJMenu);

        //把菜单栏对象放入游戏界面
        this.setJMenuBar(jMenuBar);
    }

    //初始化界面方法
    private void initJFrame() {

        //设置宽高
        this.setSize(603,680);  //  this表明本类中的set方法，从父类JFrame中继承下来

        //设置标题
        this.setTitle("拼图单机版 v1.0");

        //设置界面置顶
        this.setAlwaysOnTop(true);

        //设置页面居中
        this.setLocationRelativeTo(null);

        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //取消默认居中
        this.setLayout(null);

        //给界面加入键盘监听
        this.addKeyListener(this);
    }

    //判赢
    private void victory(){
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (i == 3 && j == 3){
                    continue;
                }
                if (data[i][j] != 4 * i + j + 1) {
                    falg = false;
                    return;
                }
            }
        }
        falg = true;
    }

    //联系方式弹框
    private void accountJFrame() {

        JDialog jDialog = new JDialog();

        JLabel jLabel = new JLabel(new ImageIcon("image/wx.jpg"));
        jLabel.setBounds(0, 0, 258, 258);
        jDialog.add(jLabel);
        jDialog.setSize(344, 344);
        jDialog.setAlwaysOnTop(true);
        jDialog.setLocationRelativeTo(null);
        jDialog.setModal(true);
        jDialog.setVisible(true);
    }

    public void replayItem(){
        initData();
        x = 3;
        y = 3;
        step = 0;
        initImage();
    }

    //键盘监听事件
    @Override
    public void keyTyped(KeyEvent e) {  //字母按键
    }

    @Override
    public void keyPressed(KeyEvent e) {   //按下
        if (falg){
            return ;
        }
        int code = e.getKeyCode();
        if (code == 32){
            //清除所有
            this.getContentPane().removeAll();

            //添加完整图片
            JLabel all = new JLabel(new ImageIcon(path+"all.jpg"));
            all.setBounds(83, 134, 420, 420);
            this.add(all);

            //添加背景图片
            JLabel background = new JLabel(new ImageIcon("image/background.png"));
            background.setBounds(40, 40, 508, 560);
            this.getContentPane().add(background);

            this.getContentPane().repaint();

        }
    }

    @Override
    public void keyReleased(KeyEvent e) {  //松开
        if (falg){
            return ;
        }
        int code = e.getKeyCode();
        if (code == 37 && y<=2) {//向左   data[x][y]是空白
            data[x][y] = data[x][y+1];
            data[x][y+1] = 0;
            y++;
            step++;
            initImage();
        }
        else if (code == 38 && x<=2) {//向上
            data[x][y] = data[x+1][y];
            data[x+1][y] = 0;
            x++;
            step++;
            initImage();
        }
        else if (code == 39 && y>=1) {//向右
            data[x][y] = data[x][y-1];
            data[x][y-1] = 0;
            y--;
            step++;
            initImage();
        }
        else if (code == 40 && x>=1) {//向下
            data[x][y] = data[x-1][y];
            data[x-1][y] = 0;
            x--;
            step++;
            initImage();
        } else if (code == 32) {
            initImage();
        } else if (code == 101) { //直接通关
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    data[i][j] = 4*i+j+1;
                }
            }
            data[3][3] = 0;
            x = 3;
            y = 3;
            initImage();
        }
    }

    //动作监听
    @Override
    public void actionPerformed(ActionEvent e) {
        Random ran = new Random();
        if (e.getSource() == replayItem){
            replayItem();
        } else if (e.getSource() == reLoginItem) {
            //隐藏游戏界面
            this.setVisible(false);
            //打开登录界面
            new LoginJFrame();

        } else if (e.getSource() == closeItem) {
            //关闭游戏
            System.exit(0);

        }else if (e.getSource() == animalItem) {
            path = "image/animal/animal"+ran.nextInt(1,9)+"/";
            System.out.println(path);
            replayItem();
        }else if (e.getSource() ==  girlItem) {
            path = "image/girl/girl"+ran.nextInt(1,14)+"/";
            System.out.println(path);
            replayItem();
        }else if (e.getSource() == sportItem) {
            path = "image/sport/sport"+ran.nextInt(1,11)+"/";
            System.out.println(path);
            replayItem();
        }else if (e.getSource() == changePicturetItem) {
            int len = path.length(), ranInt = 0;
            if (len>=21){
                ranInt = ran.nextInt(1,8);
            }else if (len <= 18){
                ranInt = ran.nextInt(1,14);
            }else {
                ranInt = ran.nextInt(1,11);
            }

            if (len == 18 || len == 20 || len == 22){
                path = path.substring(0,path.length()-3)+ranInt+"/";
            }
            else {
                path = path.substring(0,path.length()-2)+ranInt+"/";
            }
            System.out.println(path);
            replayItem();
        } else if (e.getSource() == accountItem) {
            //弹出联系方式界面
            accountJFrame();
        }
    }
}
